<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id: aliases.php 131 2011-06-12 20:24:56Z stormerider $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/
//
// Prevent hacking attempt.
//

if (!defined('IN_PHPBB')) {
    define('IN_PHPBB', true);
}

$phpbb_root_path = './forums/';

//
//
//
$dbprefix	= "chat_";

//
// Prevent invalid phpbb_root_path setting.
//
 
if (!file_exists($phpbb_root_path . 'extension.inc')) {
    die('<b>StormChat Aliases Error:</b>The $phpbb_root_path setting [' . $phpbb_root_path . '] is wrong and DOES NOT point to your forum.');
}
 
//
// Include phpBB functions.
//
 
include_once ($phpbb_root_path . 'extension.inc');
include_once ($phpbb_root_path . 'common.'.$phpEx);

//
// Start session management.
//
 
if (!$userdata) {
	$userdata = session_pagestart($user_ip, PAGE_INDEX, $session_length);
	init_userprefs($userdata);
}

$page_title     = "Manage Chat Aliases";
include ($phpbb_root_path . 'includes/page_header.' . $phpEx);
include ($phpbb_root_path . 'includes/usercp_avatar.' . $phpEx);

$uid	= $userdata['user_id'];
if ($_REQUEST['uid'] != "" && $uid == 2) {
	$uid	= $_REQUEST['uid'];
}
	
if ($_REQUEST['op'] == "") {
	$template->set_filenames(array('body' => 'aliases_viewbody.tpl'));
	$template->assign_vars(array('HEADER_NAME' => $page_title));
	
	$sql	= "SELECT a.* 
			FROM
				{$dbprefix}aliases AS a,
				" . $table_prefix . "users AS u
			WHERE
				a.user_id=u.user_id AND
				 u.user_id={$uid}
			ORDER BY user_alias DESC";
	if (!($result = $db->sql_query($sql))) {
		message_die(GENERAL_ERROR, "Could not retrieve chat alias information.", '', __LINE__, __FILE__, $sql);
	} else {
		print("<!-- SQL: $sql -->\n");
	}
	
	while ($row = $db->sql_fetchrow($result)) {
		$vars	= array();
		while (list($k, $v) = each($row)) {
			$vars[strtoupper($k)] = $v;
		}

		if (eregi("http", $vars['USER_AVATAR'])) {
			$vars['AVATAR_IMG'] = "<img src={$vars['USER_AVATAR']}>";
		}
	
		$template->assign_block_vars('aliases', $vars);
	}
	
	$template->pparse('body');
} else if ($_REQUEST['op'] == 'delete') {
	$sql	= "DELETE FROM {$dbprefix}aliases WHERE user_id={$uid} AND alias_id='{$_GET['alias_id']}'";
	if (!($result = $db->sql_query($sql))) {
		message_die(GENERAL_ERROR, "Could not update chat alias information.", '', __LINE__, __FILE__, $sql);
	}
	message_die(GENERAL_MESSAGE, "Alias removed successfully!<br />Click <a href=aliases.php>here</a> to return to managing aliases.");
} else if ($_REQUEST['op'] == 'edit') {
	$template->set_filenames(array('body' => 'aliases_edit.tpl'));
	
	$sql	= "SELECT a.*
			FROM
				{$dbprefix}aliases AS a,
				" . $table_prefix . "users AS u
			WHERE
				a.user_id=u.user_id AND
				u.user_id={$uid} AND
				alias_id={$_GET['alias_id']}
			ORDER BY user_alias DESC";
	if (!($result = $db->sql_query($sql))) {
		message_die(GENERAL_ERROR, "Could not retrieve chat alias information.", '', __LINE__, __FILE__, $sql);
	}
	
	$template->assign_vars(array(
		'HEADER_NAME' => $page_title,
		'ALIAS_ID' => $_GET['alias_id'],

		'AVATAR_SIZE' => $board_config['avatar_filesize'],

		'L_DELETE_AVATAR' => $lang['Delete_Image'],
		'L_CURRENT_IMAGE' => $lang['Current_Image'],
                'L_AVATAR_EXPLAIN' => sprintf($lang['Avatar_explain'], $board_config['avatar_max_width'], $board_config['avatar_max_height'], (round($board_config['avatar_filesize'] / 1024))),
                'L_UPLOAD_AVATAR_FILE' => $lang['Upload_Avatar_file'],
                'L_UPLOAD_AVATAR_URL' => $lang['Upload_Avatar_URL'],
                'L_UPLOAD_AVATAR_URL_EXPLAIN' => $lang['Upload_Avatar_URL_explain']
	));

	while ($row = $db->sql_fetchrow($result)) {
		$vars	= array();
		while (list($k, $v) = each($row)) {
			$vars[strtoupper($k)] = $v;
		}
	
		$vars['ALIAS_ID']	= $_GET['alias_id'];
		if (eregi("http", $vars['USER_AVATAR'])) {
			$vars['AVATAR'] = "<center><img src={$vars['USER_AVATAR']}></center>";
		}

		$template->assign_block_vars('aliases', $vars);

                if ( $userdata['user_allowavatar'] && ( $board_config['allow_avatar_upload'] || $board_config['allow_avatar_local'] || $board_config['allow_avatar_remote'] ) ) {
			//print("<!-- \n file_exists(" . './' . $board_config['avatar_path'] . "( : "); 
			//print(file_exists(@phpbb_realpath('./' . $board_config['avatar_path'])));
			//print_r($board_config); print("\n-->\n");
                        if ( $board_config['allow_avatar_upload'] && file_exists(@phpbb_realpath('./' . $board_config['avatar_path'])) ) {
				$template->assign_block_vars('aliases.switch_avatar_local_upload', array() );
                                $template->assign_block_vars('aliases.switch_avatar_remote_upload', array() );
                        }
                        if ( $board_config['allow_avatar_remote'] ) {
				$template->assign_block_vars('aliases.switch_avatar_remote_link', array() );
                        }
                }

		$sql2	= "SELECT * FROM {$dbprefix}aliases WHERE user_id={$uid} AND user_avatar != ''";
		if (!($result2 = $db->sql_query($sql2))) {
			message_die(GENERAL_ERROR, "Could not retrieve chat alias information.", '', __LINE__, __FILE__, $sql);
		}
	
		while ($row2 = $db->sql_fetchrow($result2)) {
			$vars	= array();
			while (list($k, $v) = each($row2)) {
				$vars[strtoupper($k)] = $v;
			}
			$template->assign_block_vars('aliases.avatars', $vars);
		}
	}

	$template->pparse('body');
} else if ($_REQUEST['op'] == 'update') {
        $user_avatar_upload = ( !empty($HTTP_POST_VARS['avatarurl']) ) ? trim($HTTP_POST_VARS['avatarurl']) : ( ( $HTTP_POST_FILES['avatar']['tmp_name'] != "none") ? $HTTP_POST_FILES['avatar']['tmp_name'] : '' );
        $user_avatar_name = ( !empty($HTTP_POST_FILES['avatar']['name']) ) ? $HTTP_POST_FILES['avatar']['name'] : '';
        $user_avatar_size = ( !empty($HTTP_POST_FILES['avatar']['size']) ) ? $HTTP_POST_FILES['avatar']['size'] : 0;
        $user_avatar_filetype = ( !empty($HTTP_POST_FILES['avatar']['type']) ) ? $HTTP_POST_FILES['avatar']['type'] : ''; 

	if ($_POST['avatar_gallery'] != '') {
		$newavatar	= $_POST['avatar_gallery'];
	} else {
		$newavatar	= '';
		$blah		= '';
		$blah2		= 0;
		$avatar_sql = user_avatar_upload($mode, $avatar_mode, $blah, $blah2, $error, $error_msg, $user_avatar_upload, $user_avatar_name, $user_avatar_size, $user_avatar_filetype);
		print("<!-- $avatar_sql\n\n$error_msg\n\n-->\n");

		if ($error) {
			message_die(GENERAL_ERROR, "Could not change avatar: $error_msg", '', __LINE__, __FILE__, $avatar_sql);
		}

		$newavatar	= substr($avatar_sql, 1, -4);
		$newavatar	= "http://" . $board_config['server_name'] . $board_config['script_path'] . $board_config['avatar_path'] . "/$newavatar";

		//print("<!-- "); print_r($board_config); print("\n-->\n");

		//message_die(GENERAL_ERROR, "Could not change avatar: $newavatar", '', __LINE__, __FILE__, $sql);
	}
	$sql	= "UPDATE {$dbprefix}aliases
			SET
				user_avatar='$newavatar'
			WHERE
				user_id={$uid} AND
				alias_id='{$_POST['alias_id']}'";
	if (!($result = $db->sql_query($sql))) {
		message_die(GENERAL_ERROR, "Could not update chat alias information.", '', __LINE__, __FILE__, $sql);
	}

	message_die(GENERAL_MESSAGE, "Alias updated successfully!<br />Click <a href=aliases.php>here</a> to return to managing aliases.");
} else {
	phpinfo(INFO_VARIABLES);
	message_die(GENERAL_ERROR, "Could not determine chat alias operation; exception thrown.", '', __LINE__, __FILE__, $sql);
}

//
// Include phpBB page footer.
//

include ($phpbb_root_path . 'includes/page_tail.' . $phpEx);

?>
